package cn.tth.test.game.features;

import cn.tth.test.game.bean.BeanApplicationContext;
import cn.tth.test.game.collection.MyList;
import cn.tth.test.game.common.ScannerStream;
import org.springframework.util.CollectionUtils;

import java.util.Arrays;


public abstract class BaseFeatures {

    private MyList<BaseFeatures> featuresList;

    private MyList<String> beanDefinitionNameList;

    protected void setFeaturesList(BaseFeatures... baseFeatures) {
        this.featuresList = new MyList<>(Arrays.asList(baseFeatures));
    }

    public void setBeanDefinitionNameList() {
        this.beanDefinitionNameList = new MyList<>();
        for (BaseFeatures features : featuresList) {
            beanDefinitionNameList.add(BeanApplicationContext.getDefinitionNameByClass(features.getClass()));
        }
    }

    public void initialization() {
        if(CollectionUtils.isEmpty(featuresList)){
            setFeaturesList();
        }
        if(CollectionUtils.isEmpty(beanDefinitionNameList)){
            setBeanDefinitionNameList();
        }
    }

    public void main(){
        outputCopyWriting();
        jump();
    }

    private void outputCopyWriting() {
        initialization();
        System.out.println(beanDefinitionNameList.toString());
    }

    public void jump() {
        String in = ScannerStream.getIn();
        int index = 0;
        try {
            index = Integer.parseInt(in);
        }catch (Exception e){
            ScannerStream.outError();
        }
        BaseFeatures baseFeatures = featuresList.get(index);
        try {
            baseFeatures.main();
        }catch (Exception e){
            ScannerStream.outError();
        }
    }
}
